package taskhook

import (
	"github.com/robfig/cron/v3"
	"gitlab.local/DO-module/new-filemanage-module/src/constant"
	"gitlab.local/golibrary/sysconfig"
	"gitlab.local/golibrary/utils"
	"sync"
)

type Task struct {
	Spec    string
	Fn      func()
	EntryID cron.EntryID
}

type TaskHook struct {
	ID   string
	Spec string
	Fn   func()
}

type HookApi interface {
	Persistence()
}

type HookService struct {
	list       []TaskHook
	extTask    map[string]*Task
	mapLock    sync.Mutex   //增加持久化任务锁
	lock       sync.RWMutex //注册事件锁
	running    bool
	job        *cron.Cron
	config     sysconfig.Getter
	prevZone   string
	prevServer string
}

func New(config sysconfig.Getter) *HookService {
	manage := &HookService{
		config:  config,
		extTask: map[string]*Task{},
	}
	manage.Reload()
	utils.WatchFile(constant.SysDefault, manage.Reload)
	return manage
}
